package com.example.sql_damo;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class modification extends AppCompatActivity {
    private SQLiteDatabase db;
    private DBhelpen dBhelpen;
    private EditText pas;
    private EditText pasword;
    private Button oks;
    private ContentValues values;
    private String p;
    private String pp;
    private String  name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modification);
        dBhelpen=new DBhelpen(this);
        Intent intent=getIntent();
        name=intent.getStringExtra("name");
        oks =findViewById(R.id.modification_ok);
        oks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pas =findViewById(R.id.modification_password);
                pasword=findViewById(R.id.modification_passwords);

                p =pas.getText().toString();
                pp =pasword.getText().toString();
                if (p.equals(pp)||p.length()<8){

                    initmodification(name);

                }else {
                    Toast.makeText(modification.this, "密码不一致或者密码长度小于8", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void initmodification(String name) {
        db=dBhelpen.getWritableDatabase();
        values=new ContentValues();
        values.put("PAS",p);
        db.update("INFOMATION",values,"NAME=?",new String[]{name});
        db.close();
        Toast.makeText(this, "用户："+name+"  密码修改成功", Toast.LENGTH_SHORT).show();
        Intent intent=new Intent(modification.this,MainActivity.class);
        startActivity(intent);
    }

}
